# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface


class Game4399Pipeline:

    def open_spider(self, spider):
        # 打开文件写表头
        self.file = open("data.csv", "w", encoding="utf-8-sig")

    def close_spider(self, spider):
        # 关闭文件
        self.file.close()
        print("测试这个是不是内置函数22")

    def process_item(self, item, spider):
        # zai 管道中进行数据处理
        # 打开一个文件
        # 将内容写入
        if self.file.closed:
            self.file = open("data.csv", "w", encoding="utf-8")
        self.file.write(f"{item['li_html']},{item['li_name']},{item['li_time']}\n")
        print("====>>", item)

        return item


import pymysql
class Game4399PipelineMysql:

    def open_spider(self, spider):
        # 打开文件写表头
        # 链接mysql
        self.conn = pymysql.connect(host='127.0.0.1', port=3306,user='root',password='123456')

    def close_spider(self, spider):
        # 关闭文件
        self.conn.close()
        print("测试这个是不是内置函数22")

    def process_item(self, item, spider):
        # 创建游标
        try:
            self.cursor = self.conn.cursor()
            # 写入数据
            sql = "insert into 表名（l1, 12） values (%s, %s)"
            self.cursor.execute(sql,(item[""], item[""]))
        except Exception as e:
            print(e)
            self.conn.rollback()
        return item
